﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// xinxianshi_save 的摘要说明
    /// </summary>
    public class xinxianshi_save : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //判断用户名密码是否正确
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            string _remark = DTRequest.GetFormString("txtRemark");
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"请输入您要发布的内容！\"}");
                return;
            }
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");
            //开始提交内容
            BLL.xinxianshi bll = new BLL.xinxianshi();
            Model.xinxianshi model = new Model.xinxianshi();

            model.remark = _remark;
            model.user_id = userModel.id;
            model.status = 1;
            model.add_time = DateTime.Now;
            model.hits = 0;
            model.zan = 0;
            model.comment = 0;
            model.quanzi_id = _quanzi_id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.xxs_albums> ls = new List<Model.xxs_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.xxs_albums { original_path = filePath, thumb_path = thumb_path });
                }
                model.albums = ls;
            }
            #endregion

            int result = bll.Add(model);
            if (result > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }

        /// <summary>
        /// 统一保存文件
        /// </summary>
        private string FileSave(HttpContext context, HttpPostedFile upFiles, bool isWater)
        {
            //检查是否允许匿名上传
            /*if (sysConfig.fileanonymous == 0 && !new ManagePage().IsAdminLogin() && !new BasePage().IsUserLogin())
            {
                showError(context, "禁止匿名非法上传！");
                return;
            }*/
            //获取文件信息
            string fileName = upFiles.FileName;
            byte[] byteData = FileHelper.ConvertStreamToByteBuffer(upFiles.InputStream); //获取文件流
            //开始上传
            string remsg = new UpLoad().FileSaveAs(byteData, fileName, true, isWater);
            Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(remsg);
            string status = dic["status"].ToString();
            string msg = dic["msg"].ToString();
            if (status == "0")
            {
                //showError(context, msg);
                return "";
            }
            string filePath = dic["thumb"].ToString(); //取得上传后的路径
            return filePath;
            //showSuccess(context, fileName, filePath); //输出成功提示
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}